package progressCaseBusiness;

import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetRequestInputStream;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;
import tools.UUid;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * Created by yangxianming on 2021/2/3.
 * x
 */
public class SaveProgressCase implements TestServerBusinessImpl {

    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig){
        //读取请求json
        JSONObject requestInfo = GetRequestInputStream.getJson(request);
        ReturnJson returnJson = new ReturnJson();

        //数据库操作
        System.out.println(requestInfo);
        String action = requestInfo.getString("action");

        //新建条目
        //INSERT INTO `progresscase` (`uuid`, `progressname`, `module`, `title`, `condition`, `steps`, `state`) VALUES
        // ('regfgfd234343', '口福3.4', '首页', '这个是标题', '这个是前置条件', '这个操作步骤', '1')
        if (Objects.equals("addCase", action)){
            JSONObject data = requestInfo.getJSONObject("data");
            System.out.println(data);
            String uuid = data.getString("uuid");
            String progressname = data.getString("progressname");
            String module = data.getString("module");
            String title = data.getString("title");
            String condition = data.getString("condition");
            String steps = data.getString("steps");
            String state = data.getString("state");
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);

            //通过uuid判断是否存在
            String sql = "SELECT * FROM progresscase WHERE uuid = '"+uuid+"'";
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(sql);
            if (sqlReturnModel.getSelectReturnData().size() == 0){
                //新建
                String uid = UUid.getUUid();
                String value = "('"+ uid + "', '"+progressname+"', '"+module+"', '"+title+"', '"+condition+"', '"+steps+"', '"+state+"')";
                String insertSql = "INSERT INTO `progresscase` (`uuid`, `progressname`, `module`, `title`, `conditions`, `steps`, `state`) VALUES " + value;
                sqlReturnModel = sqlDispatch.insert(insertSql);
                returnJson.setResult(uid);
            }else {
                //更新
                String updateSql = "UPDATE progresscase SET ";
                String sqlset = " progressname='" + progressname + "', ";
                sqlset = sqlset + "module='" + module + "', ";
                sqlset = sqlset + "title='" + title + "', ";
                sqlset = sqlset + "conditions='" + condition + "', ";
                sqlset = sqlset + "steps='" + steps + "', ";
                sqlset = sqlset + "state='" + state + "'";
                updateSql = updateSql + sqlset + " WHERE uuid = '"+uuid+"'";
                System.out.println(updateSql);
                sqlReturnModel = sqlDispatch.update(updateSql);
            }

            returnJson.setApiStatus((sqlReturnModel.getIsSuccess()) ? 1 : 0);
            returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        }

        //save 更新项目进度
        if (Objects.equals("update", action)){
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            JSONObject data = requestInfo.getJSONObject("data");
            String uuid = data.getString("uuid");
            String state = data.getString("state");
            String updateSql = "UPDATE progresscase SET ";
            String sqlset = "state='" + state + "'";
            updateSql = updateSql + sqlset + " WHERE uuid = '"+uuid+"'";
            System.out.println(updateSql);
            SqlReturnModel sqlReturnModel = sqlDispatch.update(updateSql);
            returnJson.setApiStatus((sqlReturnModel.getIsSuccess()) ? 1 : 0);
            returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        }


        return returnJson.toString();
    }
}
